package com.crm.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.mdao.RefundgoodMapper;
import com.crm.model.mdao.RefunditemsMapper;
import com.crm.model.mpojo.Outgood;
import com.crm.model.pojo.Refundgood;
import com.crm.services.RefundgoodService;
import com.github.pagehelper.PageInfo;

@Controller
public class RefundgoodController {
	
	@Autowired
	RefundgoodService refundgoodService;
	
	@Autowired
	RefundgoodMapper refundgoodMapper;
	
	@Autowired
	RefunditemsMapper refunditemsMapper;
	
	
	
	/**
	 * 分页跳转到发货单页
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/sales/prosale/refundgood")
	public ModelAndView order(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		ModelAndView mv = new ModelAndView("sales/prosale/refundgood");
		//分页查询订单
		List<Map<String, String>> findAllMaper = refundgoodService.selectByPager(pageNum, pageSize,statusid,search);
		//获取发货单数据
		System.out.println(findAllMaper);
		String jsonString = JSONObject.toJSONString(findAllMaper);
		//获取分页信息
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(findAllMaper);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		System.out.println(jsonString);
		mv.addObject("list", jsonString);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);		
		return mv;
	}
	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/sales/prosale/refundgood")
	@ResponseBody	
	public PageInfo<Map<String, String>> orderPage(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAll = refundgoodService.selectByPager(pageNum, pageSize,statusid,search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}
	
	
	/**
	 * 添加退货，生成退货单
	 * @return 
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/addreturnGoodsview")
	@ResponseBody
	public String addreturnGoodsview(@RequestParam("refaGood") Object proid,
			@RequestParam("oid") Integer id,
			@RequestParam("sumMoney") String sumMoney) {
		String str= String.valueOf(proid);
		System.out.println(id);
		@SuppressWarnings("unchecked")
		List<Map<String,String>> limap = JSONArray.parseObject(str,List.class);
		return refundgoodService.addreturnGoodsview(limap,id,sumMoney)+"";
	}
	
	
	/**
	 * 重定向到退货单
	 * @return 
	 * @return 
	 * @return
	 */
	@RequestMapping("/sales/prosale/refundgoodview")
	public ModelAndView refundgoodview(Integer rgid) {
		ModelAndView mv = new ModelAndView("/sales/prosale/refundgoodview");
		//查询退货
		Map<String, String> selectByogview = refundgoodMapper.selectByrgview(rgid);
		//查询退货详情
		List<Map<String, String>> selectByOgid = refunditemsMapper.selectByRgid(rgid);
		mv.addObject("refundgood", selectByogview);
		mv.addObject("refundgooditem", selectByOgid);
		mv.addObject("refundgoodit", JSONObject.toJSONString(selectByOgid));
		System.out.println("发货"+selectByogview);
		System.out.println("发货详情"+selectByOgid);
		return mv;
	}
	
	
	
	/**
	 * 修改退货单状态
	 * @return
	 */
	@PostMapping("/sales/prosale/updaterefundtype")
	@ResponseBody
	public String updateType(@RequestParam(name = "rgid", required = false) Integer rgId,
			@RequestParam(name = "oid", required = false) Integer oId) {
		com.crm.model.mpojo.Refundgood refundgood=new com.crm.model.mpojo.Refundgood();
		refundgood.setRgId(rgId);
		refundgoodService.updateType(refundgood,oId);
		return "编辑物流信息";
	}
}
